﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using laba2.Context;
using laba2.Entity;
using laba2.Repository.IRepository;
using laba2.Repository.RepositoryImpl;
using Task = laba2.Entity.Task;

namespace laba2.Services
{
    public class TaskService
    {
        /* private ITaskRepository _taskRepository;
        private ICourseRepository _courseRepository;

        public TaskService(ITaskRepository taskRepository, ICourseRepository courseRepository)
        {
        }
        */

        public TaskService()
        {
        }

        public void AddPracticalTask(string name, int maximumRating, int passingRating, string deadline,
                                     int courseId)
        {
            using (var context = new Lab3DBContext())
            {
                context.Tasks.Add(new PracticalTask(passingRating, maximumRating, name, context.Courses.Find(courseId),
                                                    DateTime.Parse(deadline)));
                //  context.PrincipleOfRatingByDates.Add(new PrincipleOfRatingByDate());
                context.SaveChanges();
            }
        }

        public void AddBoundaryControl(string name, int maximumRating, int passingRating, string date,
                                       int courseId)
        {
            using (var context = new Lab3DBContext())
            {
                context.Tasks.Add(new BoundaryControl(passingRating, name, maximumRating, context.Courses.Find(courseId),
                                                      DateTime.Parse(date)));

                context.SaveChanges();
            }
        }

        public void AddFinalControl(int passingRating, int maximumRating, string name, int courseId)
        {
            using (var context = new Lab3DBContext())
            {
                context.Tasks.Add(new FinalControl(passingRating, name, maximumRating, context.Courses.Find(courseId)));
                context.SaveChanges();
            }
        }

        public int GetRatingForDate(int taskId, string date)
        {
            using (var context = new Lab3DBContext())
            {
                var task = context.Tasks.Find(taskId);
                if (!(task is PracticalTask))
                {
                    return 0;
                }
                else
                {
                    PracticalTask practicalTask = (PracticalTask) task;
                    context.Entry(practicalTask).Reference(p => p.PrincipleOfRating).Load();
                    return practicalTask.PrincipleOfRating.GetRating(DateTime.Parse(date), practicalTask);
                }
            }

        }

        public Task GetById(int taskId)
        {
            using (var context = new Lab3DBContext())
            {

                return context.Tasks.Find(taskId);
            }

        }

        public void SetRatingByDate(int taskId, int delay, int rating)
        {
            using (var context = new Lab3DBContext())
            {
                PracticalTask practicalTask = (PracticalTask) context.Tasks.Find(taskId);

                //context.Entry(practicalTask).Reference(p => p.PrincipleOfRating).Load();
                //PrincipleOfRatingByDate principle =
                ///  context.PrincipleOfRatingByDates.Find(practicalTask.PrincipleOfRatingId);
                //principle.Add(delay,rating);
                if (!context.Entry(practicalTask).Reference(p => p.PrincipleOfRating).IsLoaded)
                {
                    context.Entry(practicalTask).Reference(p => p.PrincipleOfRating).Load();
                }
                practicalTask.PrincipleOfRating.Add(delay, rating);
                context.SaveChanges();
            }
        }

        public List<Task> GetTaskByCourse(int courseId)
        {
            using (var context = new Lab3DBContext())
            {
                return context.Tasks.Where(t => t.CourseId == courseId).ToList();
            }
        }
    }
}
